﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.BOM;
using FriendStar.Domain.IRepository.BOM;
using FriendStar.Repository.BOM;
using System.Data;

namespace FriendStar.Application.BOM
{
    /// <summary>
	/// 数据视图 V_LXK_11_ML_ProductList 对应的数据应用功能类。
	/// </summary>
	public class V_LXK_11_ML_ProductListApp
    {       
        private string UseUnit = Configs.GetValue("UseUnit");  

        private V_LXK_11_ML_ProductListRepository service = new V_LXK_11_ML_ProductListRepository();

        public List<V_LXK_11_ML_ProductListEntity> GetList(Pagination pagination, string ProductNO, string ProductName, string ProductClass, string MaintainStatus)
        {
            var expression = ExtLinq.True<V_LXK_11_ML_ProductListEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.MaintainStatus != "2");
            if (!string.IsNullOrEmpty(ProductNO))
            {
                expression = expression.And(t => t.FProductNO.Contains(ProductNO));
            }
            if (!string.IsNullOrEmpty(ProductName))
            {
                expression = expression.And(t => t.FProductName.Contains(ProductName));
            }
            if (!string.IsNullOrEmpty(ProductClass))
            {
                expression = expression.And(t => t.FProductClass == ProductClass);
            }
            if (!string.IsNullOrEmpty(MaintainStatus))
            {
                expression = expression.And(t => t.MaintainStatus == MaintainStatus);
            }
            return service.FindList(expression, pagination);
        }

        public V_LXK_11_ML_ProductListEntity GetItem(string keyValue)
        {
            var expression = ExtLinq.True<V_LXK_11_ML_ProductListEntity>();
            expression = expression.And(t => t.FProductID == keyValue);
            return service.FindEntity(expression);
        }

        public List<V_LXK_11_ML_ProductListEntity> GetProductionSumList(string FProductionNO)
        {
            var expression = ExtLinq.True<V_LXK_11_ML_ProductListEntity>();
            //expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.MaintainStatus != "2");
            expression = expression.And(t => t.FSign == "1");
            //expression = expression.And(t => t.FProductNO == ProductNO);
            Pagination pagination = new Pagination() { rows = int.MaxValue, sidx = "UseUnit, FProductNO", sord = "ASC,ASC" };
            List<V_LXK_11_ML_ProductListEntity> list = service.FindList(expression, pagination);

            string sql = string.Format("SELECT * FROM  LXK_13_LX_ProductListIn WHERE UseUnit ='{0}' and FProductionNO ='{1}' AND StartDate<='{2}' AND  StopDate >='{2}'", UseUnit, FProductionNO, DateTime.Now.ToString("yyyy-MM-dd"));
            DataTable dt = service.GetDataTable(sql, null);
            foreach (var item in list)
            {
                if (item.FOrderNum.HasValue && item.FOrderNum > 0)
                {
                    string title = "ProductSum" + item.FOrderNum.Value.ToString("0").PadLeft(3, '0');
                    if (dt.Rows.Count > 0 && dt.Columns.Contains(title))
                    {
                        int sum = 0;
                        if (dt.Rows[0][title] != DBNull.Value)
                        {
                            int.TryParse(dt.Rows[0][title].ToString(), out sum);
                        }
                        if (sum > 0)
                        {
                            item.ProductSum = sum;
                        }
                    }
                }
            }
            return list;
        }
    }
}